<?php
namespace Admin\Controller;
use \Org\Util\Collection;
class CollectionController extends CommonController
{


    private $db;

    //HTML标签
    private $html_tag = array("<p([^>]*)>(.*)</p>[|]"=>'<p>', "<a([^>]*)>[|]"=>'<a>',"</a>[|]"=>'</a>',"<h1([^>]*)>[|]"=>'<h1>',"</h1>[|]"=>'</h1>',"<script([^>]*)>(.*)</script>[|]"=>'<script>', "<iframe([^>]*)>(.*)</iframe>[|]"=>'<iframe>', "<table([^>]*)>(.*)</table>[|]"=>'<table>', "<span([^>]*)>(.*)</span>[|]"=>'<span>', "<b([^>]*)>(.*)</b>[|]"=>'<b>', "<img([^>]*)>[|]"=>'<img>', "<object([^>]*)>(.*)</object>[|]"=>'<object>', "<embed([^>]*)>(.*)</embed>[|]"=>'<embed>', "<param([^>]*)>(.*)</param>[|]"=>'<param>', '<div([^>]*)>[|]'=>'<div>', '</div>[|]'=>'</div>', '<!--([^>]*)-->[|]'=>'<!-- -->','[reset]要换的中文[|]改成的中文'=>'替换中文');

    //网址类型
    private $url_list_type = array();

    public function _initialize()
    {
        parent::_initialize();
        $this->db = D("CollectionNode");
        $this->assign('html_tag',$this->html_tag);
        $this->assign('url_list_type', array('1'=>'序列网址', '2'=>'多条网址', '3'=>'单一网页', '4'=>'RSS'));

    }
    private function listinfo($obj,$where,$pagesize,$page,$order){
        $count = $obj->where($where)->count();
        $pageObj = new \Org\Util\Page($count,$pagesize);
        $list    = $obj->where($where)->order($order)->page($page,$pageObj->listRows)->select();
        $lists   = array('lists'=>$list,'pages'=>$pageObj->show());
        return $lists;
    }
    public function _before_index(){
        $this->assign('list_table',true);
        $this->_data = array(
            'test'    => array(
                'c' => 'Collection',
                'a' => 'publishTest',
                'str'    => '<a data-height="410" data-width="520" data-id="test" data-uri="%s" data-title="采集测试" class="J_showdialog" href="javascript:;">测试</a>',
                'param' => array('nodeid'=>'@id@'),
                'isajax' => 1,
                'replace'=> ''
            ),
            'copy'    => array(
                'c' => 'Collection',
                'a' => 'colCopy',
                'str'    => '<a data-height="410" data-width="520" data-id="copy" data-uri="%s" data-title="复制" class="J_showdialog" href="javascript:;">复制</a>',
                'param' => array('nodeid'=>'@id@'),
                'isajax' => 1,
                'replace'=> ''
            ),
        );
    }
    /**
     * node list
     */
    public function index() {
        $page = I('get.p',1,'int');
        $lists = $this->listinfo($this->db,$where=array(),20,$page, 'nodeid DESC');
        $pages = $lists['pages'];
        $nodelist = $lists['lists'];
        $this->options = $this->check();
        $this->assign('pages',$pages);
        $this->assign('lists',$nodelist);
        $this->display();
    }

    private function get_one($obj,$where=array(),$field='*'){
    return $obj->field($field)->where($where)->find();
}
    /**
     * add node
     */
    public function add() {
        header("Cache-control: private");
        if(IS_POST) {
            $data = isset($_POST['data']) ? $_POST['data'] :  $this->error('参数错误');
            $customize_config = isset($_POST['customize_config']) ? $_POST['customize_config'] :  '';
            if (!$data['name'] = trim($data['name'])) {
                $this->error('请填写采集点名称');
            }
            if ($this->get_one($this->db,array('name'=>$data['name']))) {
                $this->error('采集点名称已存在');
            }
            $data['urlpage'] = isset($_POST['urlpage'.$data['sourcetype']]) ? $_POST['urlpage'.$data['sourcetype']] : $this->error('请填写网址规则');
            $data['customize_config'] = array();
            if (is_array($customize_config)) foreach ($customize_config['en_name'] as $k => $v) {
                if (empty($v) || empty($customize_config['name'][$k])) continue;
                $data['customize_config'][] = array('name'=>$customize_config['name'][$k], 'en_name'=>$v, 'rule'=>$customize_config['rule'][$k], 'html_rule'=>$customize_config['html_rule'][$k]);
            }
            $data['customize_config'] = array2string($data['customize_config']);
            if ($this->db->add($data)) {
                $this->success('添加采集点成功',U('Collection/index'));
            } else {
                $this->error('添加采集点失败');
            }
        } else {
           $this->display();
        }

    }

    //修改采集配置
    public function edit() {
        $nodeid = I('request.id',0,'int');
        if(!$nodeid) $this->error('参数错误');
        $data = $this->get_one($this->db,array('nodeid'=>$nodeid));
        if(IS_POST) {
            $datas = $data;
            unset($data);
            $data = isset($_POST['data']) ? $_POST['data'] :  $this->error('参数错误');
            $customize_config = isset($_POST['customize_config']) ? $_POST['customize_config'] :  '';
            if (!$data['name'] = trim($data['name'])) {
                $this->error('请填写采集点名称');
            }

            if ($datas['name'] != $data['name']) {
                if ($this->get_one($this->db,array('name'=>$data['name']))) {
                    $this->error('采集点名称已存在');
                }
            }

            $data['urlpage'] = isset($_POST['urlpage'.$data['sourcetype']]) ? $_POST['urlpage'.$data['sourcetype']] : $this->error('请填写网址规则');
            $data['customize_config'] = array();
            if (is_array($customize_config)) foreach ($customize_config['en_name'] as $k => $v) {
                if (empty($v) || empty($customize_config['name'][$k])) continue;
                $data['customize_config'][] = array('name'=>$customize_config['name'][$k], 'en_name'=>$v, 'rule'=>$customize_config['rule'][$k], 'html_rule'=>$customize_config['html_rule'][$k]);
            }
            $data['customize_config'] = array2string($data['customize_config']);
            if ($this->db->where(array('nodeid'=>$nodeid))->save($data)) {
                $this->success('修改成功',U('Collection/index',array('menuid'=>230)));
            } else {
               $this->error('修改失败');
            }
        } else {
            if (isset($data['customize_config'])) {
                $data['customize_config'] = string2array($data['customize_config']);
            }
         $this->assign('info',$data);
         $this->display();
        }
    }

    //复制采集
    public function colCopy() {
        $nodeid = I('request.nodeid',0,'int');
        if(!$nodeid) $this->ajaxReturn(0,'参数错误');
        if ($data = $this->get_one($this->db,array('nodeid'=>$nodeid))) {
            if (IS_POST) {
                unset($data['nodeid']);
                $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : $this->ajaxReturn(0,'请填写采集点名称');
                if ($this->get_one($this->db,array('name'=>$name), 'nodeid')) {
                    $this->ajaxReturn(0,'采集点已存在');
                }
                $data['name'] = $name;
                //$data = new_addslashes($data);
                if ($this->db->add($data)) {
                  $this->ajaxReturn(1,'复制成功','','copy');
                } else {
                   $this->ajaxReturn(0,'复制失败');
                }
            } else {
                $this->assign('name',$data['name']);
                $this->assign('id',$data['nodeid']);
               $response = $this->fetch();
                $this->ajaxReturn(1,'',$response);
            }
        } else {
            showmessage(L('notfound'));
        }
    }

    //URL配置显示结果
    public function publishurl() {
        $sourcetype = isset($_GET['sourcetype']) && intval($_GET['sourcetype']) ? intval($_GET['sourcetype']) : $this->ajaxReturn(0,'参数错误');
        $pagesize_start = isset($_GET['pagesize_start']) && intval($_GET['pagesize_start']) ? intval($_GET['pagesize_start']) : 1;
        $pagesize_end = isset($_GET['pagesize_end']) && intval($_GET['pagesize_end']) ? intval($_GET['pagesize_end']) : 10;
        $par_num = isset($_GET['par_num']) && intval($_GET['par_num']) ? intval($_GET['par_num']) : 1;
        $urlpage = isset($_GET['urlpage']) && trim($_GET['urlpage']) ? trim($_GET['urlpage']) : $this->ajaxReturn(0,'url地址错误');
        $this->assign('sourcetype',$sourcetype);
        $this->assign('pagesize_start',$pagesize_start);
        $this->assign('pagesize_end',$pagesize_end);
        $this->assign('par_num',$par_num);
        $this->assign('urlpage',$urlpage);
        $response = $this->fetch();
        $this->ajaxReturn(1, '', $response);
    }
    public function delete(){
        $mod = $this->db;
        $pk = $mod->getPk();
        $ids = trim(I('id'), ',');
        if ($ids) {
            if (false !== $mod->where(array('nodeid'=>array('in',$ids)))->delete()) {
                $content_db = D('CollectionContent');
                $content_db->where(array('nodeid'=>array('in',$ids)))->delete();
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }
    }

    //测试文章URL采集
    public function publishTest() {
        $nodeid = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : $this->error('参数错误');
        if ($data = $this->get_one($this->db,array('nodeid'=>$nodeid))) {
            $urls = Collection::url_list($data, 1);
            if (!empty($urls)) foreach ($urls as $v) {
                $url = Collection::get_url_lists($v, $data);
            }
            $this->assign('url',$url);
            $this->assign('nodeid',$data['nodeid']);
            $response = $this->fetch();
            $this->ajaxReturn(1, '', $response);
        } else {
            $this->ajaxReturn(0,'采集点不存在');
        }
    }

    //测试文章内容采集
    public function publicTestContent() {
        $url = isset($_GET['url']) ? urldecode($_GET['url']) : '';
        if(!$url) $this->ajaxReturn(0,'url地址不存在');
        $nodeid = I('request.nodeid',0,'int');
        if(!$nodeid) $this->ajaxReturn(0,'参数错误');
        if ($data = $this->get_one($this->db,array('nodeid'=>$nodeid))) {
            $contents = Collection::get_content($url, $data);
            foreach ($contents as $_key=>$_content) {
                if(trim($_content)=='') $contents[$_key] = '◆◆◆◆◆◆◆◆◆◆'.$_key.' empty◆◆◆◆◆◆◆◆◆◆';
            }
           print_r($contents);
        } else {
            $this->ajaxReturn(0,'数据不存在');
        }
    }

    //采集网址
    public function colUrl() {
        $nodeid = I('request.id',0,'int');
        if(!$nodeid){
            $this->error('参数错误');
        }
        if ($data = $this->get_one($this->db,array('nodeid'=>$nodeid))) {
            $urls = Collection::url_list($data);
            $total_page = count($urls);
            if ($total_page > 0) {
                $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
                $url_list = $urls[$page];
                $url = Collection::get_url_lists($url_list, $data);
                $history_db =D('CollectionHistory');
                $content_db = D('CollectionContent');
                $total = count($url);
                $re = 0;
                if (is_array($url) && !empty($url)) foreach ($url as $v) {
                    if (empty($v['url']) || empty($v['title'])) continue;
                    $v = new_addslashes($v);
                    $v['title'] = strip_tags($v['title']);
                    $md5 = md5($v['url']);
                    if (!$this->get_one($history_db,array('md5'=>$md5))) {
                        $history_db->add(array('md5'=>$md5));
                        $content_db->add(array('nodeid'=>$nodeid, 'status'=>0, 'url'=>$v['url'], 'title'=>$v['title']));
                    } else {
                        $re++;
                    }
                }
                if ($total_page <= $page) {
                    $this->db->where( array('nodeid'=>$nodeid))->save(array('lastdate'=>time()));
                }
                if($total_page == $page){
                    $this->success('网址采集完成，即将采集内容',U('Collection/colContent',array('id'=>$nodeid,'status'=>2)),5);
                }
                $this->assign('url_list',$url_list);
                $this->assign('total',$total);
                $this->assign('page',$page);
                $this->assign('re',$re);
                $this->assign('total_page',$total_page);
                $this->assign('nodeid',$nodeid);
                $this->assign('url',$url);
               $this->display();
            } else {
                $this->error('网址列表为空');
            }
        } else {
           $this->error('采集点不存在');
        }
    }

    //采集文章
    public function colContent() {
        $nodeid = I('request.id',0,'int');
        if(!$nodeid) $this->error('参数错误');
        if ($data = $this->get_one($this->db,array('nodeid'=>$nodeid))) {
            $content_db = D('CollectionContent');
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
            $total = isset($_GET['total']) ? intval($_GET['total']) : 0;
            if (empty($total)) $total = $content_db->where(array('nodeid'=>$nodeid, 'status'=>0))->count();
            $total_page = ceil($total/2);
            $list = $content_db->where(array('nodeid'=>$nodeid, 'status'=>0))->field('id,url')->order('id desc')->limit(2)->select();
            $i = 0;
            if (!empty($list) && is_array($list)) {
                foreach ($list as $v) {
                    $GLOBALS['downloadfiles'] = array();
                    $html = Collection::get_content($v['url'], $data);
                    $content_db->where(array('id'=>$v['id']))->save(array('status'=>1, 'data'=>array2string($html)));
                    $i++;
                }
            } else {
                $this->error('请先采集网址');
            }

            if ($total_page > $page) {
                $url = U('Collection/colContent',array('page'=>($page+1),'id'=>$nodeid,'total'=>$total));
                $this->success('采集正在进行中，采集进度:'.($i+($page-1)*2).'/'.$total,$url,1);
            } else {
                $this->db->where(array('nodeid'=>$nodeid))->save(array('lastdate'=>time()));
               $this->success('采集成功',U('Collection/index',array('menuid'=>230)));
            }
        }
    }

    //文章列表
    public function publish() {
        $nodeid = I('request.id',0,'int');
        if(!$nodeid) $this->error('参数错误');
        $node = $this->get_one($this->db,array('nodeid'=>$nodeid), 'name');
        $content_db = D('CollectionContent');
        $status = I('get.status',0,'int');
        $page = I('get.page',1,'int');
        $sql = array('nodeid'=>$nodeid);
        if ($status) {
            $sql['status'] = $status - 1;
        }
        $this->assign('list_table',true);
        $data = $this->listinfo($content_db,$sql,20,$page,'id desc');
        $this->assign('nodeid',$nodeid);
        $this->assign('name',$node['name']);
        $this->assign('lists',$data['lists']);
        $this->assign('pages',$data['pages']);
        $this->assign('status',$status);
        $this->display();
    }

    //导入文章
    public function import() {
        $nodeid = I('get.nodeid',0,'int');
        if(!$nodeid) $this->ajaxReturn(0,'参数错误');
        $id = I('id');
        $type = I('get.type','');
        $programid = I('get.programid',0,'int');
        if ($type == 'all') {

        } else {
            $ids = urldecode($id);
        }
        $program_db = D('CollectionProgram');
        $program_list = $program_db->where(array('nodeid'=>$nodeid))->field('id, catid')->select();
        $node_data = $this->get_one($this->db,array('nodeid'=>$nodeid), "customize_config");
        $node_data['customize_config'] = string2array($node_data['customize_config']);
        $node_field = array(''=>'请选择','title'=>'标题', 'editor'=>'作者', 'source'=>'来源', 'content'=>'内容');
        if (is_array($node_data['customize_config'])) foreach ($node_data['customize_config'] as $k=>$v) {
            if (empty($v['en_name']) || empty($v['name'])) continue;
            $node_field[$v['en_name']] = $v['name'];
        }
        $this->assign('nodeField',$node_field);
        $fields = array('title'=>'标题','desc'=>'摘要','info'=>'内容','comefrom'=>'来源','editor'=>'作者');
        $this->assign('model',$fields);
        $this->assign('ids',$ids);
        $this->assign('type',$type);
        $this->assign('nodeid',$nodeid);
        $this->assign('programList',$program_list);
        $this->assign('programid',$programid);
        $this->display();
    }
//添加导入方案
    public function importProgramAdd() {
        $nodeid = I('request.nodeid',0,'int');
        $ids = I('request.ids','');
        $catid = I('request.cateid',0,'int');
        $type = I('request.type','');
        if(!$nodeid){
            $this->error('采集点不存在');
        }
        if(!$catid){
            $this->error('请选择分类');
        }
        if(IS_POST){
            if($id=$this->addProgram($nodeid,$catid)){
                $this->success('添加发布方案成功',U('Collection/import',array('programid'=>$id,'nodeid'=>$nodeid,'type'=>$type,'id'=>urlencode($ids))));
            }else{
                $this->error('添加失败');
            }
        }
    }

    //删除文章
    public function contentdel() {
        $mod = D('CollectionContent');
        $pk = $mod->getPk();
        $ids = trim(I('id'), ',');
        $history = I('get.history',0,'int');
        if ($ids) {
            if($history){
                $history_db = D("CollectionHistory");
               $list = $mod->where(array('id'=>array('in',$ids)))->field('url')->select();
                $md5 = array();
                foreach ($list as $v) {
                    $md5[] = md5($v['url']);
                }
                $history_db->where(array('md5'=>array('in',$md5)))->delete();
            }
            if (false !== $mod->where(array('id'=>array('in',$ids)))->delete()) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }

    }

    public function importProgramDel() {
        $id = I('get.id',0,'int');
        if(!$id){
            $this->error('参数错误');
        }
        $program_db = D('CollectionProgram');
        if ($program_db->where(array('id'=>$id))->delete()) {
            $this->success('删除成功', HTTP_REFERER);
        } else {
           $this->error('删除失败');
        }
    }

    //导入文章到模型
    public function importContent() {
        $nodeid = I('request.nodeid',0,'int');
        $programid = I('request.programid',0,'int');
        $ids = isset($_GET['ids']) ? $_GET['ids'] : '';
        $type = isset($_GET['type']) ? trim($_GET['type']) : '';
        $starttype = I('get.stime',1,'int');//时间类型
        $startTime = I('get.start',0);//开始时间
        $sendnum = I('get.sendnum',1,'int');//发布篇数
        $random  = isset($_GET['random']) ? I('get.random',0,'int') : 0;
        if($sendnum==1 || $sendnum==2 || $sendnum==4 || $sendnum==5){
            $pagesize = 20;
        }elseif($sendnum==3 || $sendnum==6){
            $pagesize = 21;
        }else{
            $pagesize = $sendnum*3;
        }

        if (!$node = $this->get_one($this->db,array('nodeid'=>$nodeid), 'coll_order,content_page')) {
           $this->error('采集点不存在',U('Collection/index',array('menuid'=>230)));
        }
        if(!$programid){
           $this->error('方案id不存在');
        }
        if(!$startTime){
            switch($starttype){
                case 1:
                    $startTime = time();
                    break;
                case 2:
                    $startTime = strtotime(I('get.time_start'));
                    break;
                default:
                    $startTime = time();
                    break;
            }
        }

        $program_db = D('CollectionProgram');
        $collection_content_db = D('CollectionContent');
        $content_db = D('Article');

        $order = $node['coll_order'] == 1 ? 'id desc' : '';
        $str = '导入成功';
        $url = U('Collection/publish',array('id'=>$nodeid,'status'=>2));
        if ($type == 'all') {
            $total = isset($_GET['total']) && intval($_GET['total']) ? intval($_GET['total']) : '';
            if (empty($total)) $total = $collection_content_db->where(array('nodeid'=>$nodeid, 'status'=>1))->count();
            $total_page = ceil($total/$pagesize);
            $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
            $total_page = ceil($total/$pagesize);
            $data = $collection_content_db->where(array('nodeid'=>$nodeid, 'status'=>1))->order($order)->field('id,data')->limit($pagesize)->select();

        } else {
           // $ids = explode(',', $ids);
           // $ids = implode('\',\'', $ids);
            $data = $collection_content_db->where(array('id'=>array('in',$ids),'nodeid'=>$nodeid,'status' =>1))->field('id,data')->order($order)->select();
            $total = count($data);
            $str = $total.'篇文章被导入';
        }
        $program = $program_db->where(array('id'=>$programid))->find();
        $program['config'] = string2array($program['config']);
       /* $_POST['add_introduce'] = $program['config']['add_introduce'];
        $_POST['introcude_length'] = $program['config']['introcude_length'];
        $_POST['auto_thumb'] = $program['config']['auto_thumb'];
        $_POST['auto_thumb_no'] = $program['config']['auto_thumb_no'];
        $_POST['spider_img'] = 0;*/
        $i = 0;
        $n = 0;
        $coll_contentid = array();
        $catearr = array();
        if($random == 1){
            $catelist = D("ArticleCate")->where(array('status'=>1))->field('id')->select();
            foreach($catelist as $cat){
                $catearr[$cat['id']] = $cat['id'];
            }
        }

        foreach ($data as $k=>$v) {
            $cate_id = $random == 1 ? array_rand($catearr) : $program['catid'];
            $sql = array('cate_id'=>$cate_id, 'status'=>$program['config']['content_status']);
            $v['data'] = string2array($v['data']);

            foreach ($program['config']['map'] as $a=>$b) {
                if (isset($program['config']['funcs'][$a]) && function_exists($program['config']['funcs'][$a])) {
                    $GLOBALS['field'] = $a;
                    $sql[$a] = $program['config']['funcs'][$a]($v['data'][$b]);
                } else {
                    $sql[$a] = $v['data'][$b];
                }
            }
            $title = trim(strip_tags($v['data']['title']));
            $content = trim(strip_tags($v['data']['content']));

            if($k%$sendnum == 0 && $k>0){
                $n++;
            }
            $start    = strtotime(date("Y-m-d",strtotime("+".$n." day",$startTime)));
           $adddata = $this->setData($sql,$program['config']);
           // if ($node['content_page'] == 1) $sql['paginationtype'] = 2;
            $adddata['add_time'] = $this->_setTime($start);
            if($content && $title){
                $content_db->add($adddata);
                $contentid = $content_db->getLastInsID();
                if ($contentid) {
                    $coll_contentid[] = $v['id'];
                    $i++;

                } else {
                    $collection_content_db->where(array('id'=>$v['id']))->delete();
                }
            }

        }
        $sql_id = implode(',', $coll_contentid);
        $collection_content_db->where(array('id'=>array('in',$sql_id)))->save(array('status'=>2));
        if ($type == 'all' && $total_page > $page) {
            $str = '正在导入中，导入进度：'.(($page-1)*$pagesize+$i).'/'.$total;
            $url = U('Collection/importContent',array('start'=>strtotime("+1 days",$start),'random'=>$random,'sendnum'=>$sendnum,'nodeid'=>$nodeid,'programid'=>$programid,'type'=>'all','page'=>($page+1),'total'=>$total));
        }
       $this->success($str,$url);
    }
    private function setData($data,$config){
        if($config['add_introduce'] == 1){
            $len = $config['introcude_length'];
            $content = stripslashes($data['info']);
            $data['desc'] = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,$len);
            $data['desc'] = addslashes($data['desc']);
        }
        if($config['auto_thumb'] == 1){
            $num = $config['auto_thumb_no'];
            $num = $num - 1;
            $content = stripslashes($data['info']);
            if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
                $data['img'] = $matches[3][$num];
            }
        }
//       $data['cate_alias'] = getCateAlias($data['cate_id']);

        return $data;
    }
    private function _setTime($start){

        $s = strtotime(date("Y-m-d 08:00:00",$start));
        $e = strtotime(date("Y-m-d 18:00:00",$start));

        // $this->_setTime($start,$key,$nums);
        return mt_rand($s,$e);
    }
    private function addProgram($nodeid,$catid){
        $config = array();
        $model_field = isset($_POST['model_field']) ? $_POST['model_field'] : '';
        $node_field = isset($_POST['node_field']) ? $_POST['node_field'] : '';
        $funcs = isset($_POST['funcs']) ? $_POST['funcs'] : array();
        if(!$model_field){
            $this->error('原数据表字段不存在');
        }
        if(!$node_field){
            $this->error('采集字段不存在');
        }
        $config['add_introduce'] = isset($_POST['add_introduce']) && intval($_POST['add_introduce']) ? intval($_POST['add_introduce']) : 0;
        $config['auto_thumb'] = isset($_POST['auto_thumb']) && intval($_POST['auto_thumb']) ? intval($_POST['auto_thumb']) : 0;
        $config['introcude_length'] = isset($_POST['introcude_length']) && intval($_POST['introcude_length']) ? intval($_POST['introcude_length']) : 0;
        $config['auto_thumb_no'] = isset($_POST['auto_thumb_no']) && intval($_POST['auto_thumb_no']) ? intval($_POST['auto_thumb_no']) : 0;
        $config['content_status'] = isset($_POST['content_status']) && intval($_POST['content_status']) ? intval($_POST['content_status']) : 1;

        foreach ($node_field as $k => $v) {
            if (empty($v)) continue;
            $config['map'][$model_field[$k]] = $v;
        }

        foreach ($funcs as $k=>$v) {
            if (empty($v)) continue;
            $config['funcs'][$model_field[$k]] = $v;
        }

        $data = array('config'=>array2string($config), 'nodeid'=>$nodeid, 'modelid'=>0, 'catid'=>$catid);
        $program_db = D('CollectionProgram');
        if ($id = $program_db->add($data)) {
           return $program_db->getLastInsID();
        } else {
           return false;
        }
    }
}